package com.anxinr.excptionhandle;


import com.anxinr.enums.ExceptionEnum;
import com.anxinr.exception.LogicException;
import com.anxinr.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局统一异常处理
 * @author anxinr
 */
@Slf4j
@RestControllerAdvice
public class ExceptionHandler {
    /**
     * 未知异常
     *
     * @return*/
    @org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
    public Result handleGlobalException(Exception e, HttpServletRequest request) {
        log.error("捕捉到未知异常 -> {}",e);
        return Result.fail(ExceptionEnum.STUDY_ERROR.getMassage(),ExceptionEnum.STUDY_ERROR.getCode()) ;
    }
    /**
     * 逻辑异常
     *
     * @return*/
    @org.springframework.web.bind.annotation.ExceptionHandler(LogicException.class)
    public Result handleGlobalException(LogicException e, HttpServletRequest request) {
        log.error("捕捉到逻辑异常 -> {}",e);
        return Result.fail(e.getMessage(),e.getCode()) ;
    }
}
